package com.gc.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import com.gc.business.domain.InventoryInfo;
import com.gc.business.domain.PartApplication;
import com.gc.business.service.IInventoryInfoService;
import com.gc.business.service.IPartApplicationService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.web.base.BaseController;
import com.gc.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 配件申请 控制层
 * 
 * @author liujinteng
 * @date 2020-12-03
 */
@Slf4j
@Controller
@RequestMapping("/business/partApplication")
public class PartApplicationController extends BaseController {
    private String prefix = "business/partApplication";

	@Autowired
	private ISysUserService sysUserService;
	
	private final IPartApplicationService partApplicationService;

	private final IInventoryInfoService inventoryInfoService;

	@Autowired
	public PartApplicationController(IPartApplicationService partApplicationService,
									 IInventoryInfoService inventoryInfoService) {
		this.partApplicationService = partApplicationService;
		this.inventoryInfoService =  inventoryInfoService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:partApplication:view")
	@GetMapping
	public String partApplication(){
	    return prefix + "/partApplication";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{partId}")
	public String edit(@PathVariable("partId") Long partId, ModelMap mmap){
		PartApplication partApplication = partApplicationService.getById(partId);
		mmap.put("partApplication", partApplication);
		return prefix + "/edit";
	}
	
	/**
	 * 查询配件申请列表
	 */
	@RequiresPermissions("business:partApplication:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(PartApplication partApplication){
		if(sysUserService.ifCompanyRole(getAttrId())){
			InventoryInfo inventoryInfo = new InventoryInfo().setAttrId(getAttrId());
			List<InventoryInfo> infoList = inventoryInfoService.list(inventoryInfo);
			if (CollUtil.isEmpty(infoList) || infoList.size() == 0){
				return Result.success();
			}
			List<Long> collect = infoList.stream().map(InventoryInfo::getInventoryId).collect(Collectors.toList());
			startPage();
			List<PartApplication> list = partApplicationService.list(partApplication,collect);
			return Result.success(getDataTable(list));
		}else {
			return Result.success(getDataTable(new ArrayList<PartApplication>()));
		}
	}

	/**
	 * 新增保存配件申请
	 */
	@RequiresPermissions("business:partApplication:add")
	@Log(title = "配件申请", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(PartApplication partApplication){
		partApplicationService.save(partApplication);
		return Result.success();
	}

	/**
	 * 修改保存配件申请
	 */
	@RequiresPermissions("business:partApplication:edit")
	@Log(title = "配件申请", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(PartApplication partApplication){
		partApplicationService.updateById(partApplication);
		return Result.success();
	}
	
	/**
	 * 删除配件申请
	 */
	@RequiresPermissions("business:partApplication:remove")
	@Log(title = "配件申请", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		partApplicationService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出配件申请列表
	 */
	@RequiresPermissions("business:partApplication:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(PartApplication partApplication){
		List<PartApplication> list = new ArrayList<>();
		if(sysUserService.ifCompanyRole(getAttrId())){
			InventoryInfo inventoryInfo = new InventoryInfo().setAttrId(getAttrId());
			List<InventoryInfo> infoList = inventoryInfoService.list(inventoryInfo);
			if (CollUtil.isEmpty(infoList) || infoList.size() == 0){
				return Result.success();
			}
			List<Long> collect = infoList.stream().map(InventoryInfo::getInventoryId).collect(Collectors.toList());
			startPage();
			list = partApplicationService.list(partApplication,collect);
		}
		ExcelUtil<PartApplication> util = new ExcelUtil<>(PartApplication.class);
		util.exportExcel(list, "partApplication");
		return Result.success();
	}
}
